<?php
namespace yan\hkecship\box;
class Packing{
	public $items = [];
	public function addItem($x, $y, $z, $weight){
		$this->items[] = new Item($x, $y, $z, $weight);
	}
	public function clean(){
		$this->items = [];
	}
	public function run(){
		$items = $this->items;
		usort($items, function($a, $b) {
		    return $a->getMax() - $b->getMax() > 0 ? 1 : -1;
		});
		$boxes = [];
		$boxes[] = new Box();
		foreach ($items as $key => $item) {
			foreach ($boxes as $k => $box) {
				if($box->canAdd($item)){
					$box->push($item);
				}else{
					$n_box = new Box();
					if($n_box->canAdd($item)){
						$n_box->push($item);
						$boxes[] = $n_box;
					}else{
						$item->is_over = 1;
					}
				}
			}
		}
		unset($items);
		return $boxes;
	}
}